 package org.mybatis.core.typehandler;
 
 import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.core.jdbc.SQLStatement;
 
 public class StringToListTypeHandler
   implements TypeHandler<List<String>>
 {
   public static List<String> getStringToList(String text)
   {
     if (SQLStatement.isEmpty(text)) {
       return Collections.emptyList();
     }
     return Arrays.asList(text.split(","));
   }
 
   public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType)
     throws SQLException
   {
     StringBuffer para = new StringBuffer(64);
     for (String item : parameter) {
       para.append(",").append(item);
     }
     if (para.length() == 0)
       ps.setNull(i, 12);
     else
       ps.setString(i, para.substring(1));
   }
 
   public List<String> getResult(ResultSet rs, String columnName)
     throws SQLException
   {
     return getStringToList(rs.getString(columnName));
   }
 
   public List<String> getResult(ResultSet rs, int columnIndex)
     throws SQLException
   {
     return getStringToList(rs.getString(columnIndex));
   }
 
   public List<String> getResult(CallableStatement cs, int columnIndex)
     throws SQLException
   {
     return getStringToList(cs.getString(columnIndex));
   }
 }

